package com.yunhoo.zh.db.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.base.model.Department;
import com.yunhoo.base.model.Organization;
import com.yunhoo.base.model.Person;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.zh.model.UserAccount;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 用户帐套实现.
 *
 * @author Administrator
 */
public class UserAccountImpl {

    private static final String NAMESPACE = "userAccount.";

    /**
     * 查询帐套列表.<br>
     *
     * @param id_u Long 用户id
     * @return List<UserAccount>
     */
    public static List<UserAccount> queryListByUserId(Long id_u) {

        return null;
    }

    public static int initAccount(UserAccount userAccount) {

        SqlMapClient sqlmap = IbatisTemplate.getSqlMapClient();
        try {
            sqlmap.startTransaction();

            String orgCode = getMaxOrgCode(sqlmap);
            userAccount.setOrgCode(orgCode);

            insertUserAccount(sqlmap, userAccount);

            insertOrganization(sqlmap, orgCode, userAccount.getOrgName());

            insertDepartment(sqlmap, orgCode);

            insertPerson(sqlmap, orgCode);

            sqlmap.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(RegisterImpl.class.getName()).log(Level.SEVERE, null, ex);

            /* 插入异常，返回失败标识 */
            return -1;
        } finally {
            try {
                sqlmap.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(RegisterImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }

        /* 保存成功，返回标识 */
        return 1;
    }

    private static void insertUserAccount(SqlMapClient sqlmap, UserAccount userAccount) throws SQLException {
        sqlmap.insert(NAMESPACE + "insertUserAccount", userAccount);
    }

    private static void insertOrganization(SqlMapClient sqlmap, String orgCode, String orgName) throws SQLException {
        Organization org = new Organization();
        org.setOrgCode(orgCode);
        org.setOrgName(orgName);
        org.setGrade(1);
        org.setIsEnd(1);
        sqlmap.insert(NAMESPACE + "insertOrganization", org);
    }

    private static void insertDepartment(SqlMapClient sqlmap, String orgCode) throws SQLException {
        Department dept = new Department();
        dept.setOrgCode(orgCode);
        dept.setCode("01");
        dept.setName("部门01");
        dept.setIyear(2013);
        dept.setGrade(1);
        dept.setIsEnd(1);
        sqlmap.insert(NAMESPACE + "insertDepartment", dept);
    }

    private static void insertPerson(SqlMapClient sqlmap, String orgCode) throws SQLException {
        Person per = new Person();
        per.setOrgCode(orgCode);
        per.setCpsn_number("00001");
        per.setCpsn_name("name");
        sqlmap.insert(NAMESPACE + "insertPerson", per);
    }

    private static String getMaxOrgCode(SqlMapClient sqlmap) throws SQLException {
        Integer maxCode = (Integer) sqlmap.queryForObject(NAMESPACE + "getMaxOrgCode");
        return String.format("%03d%n", maxCode == null ? 1 : maxCode + 1);
    }
}
